package org.execute.tool.根据权重获取数据;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class WeightObject {
    private String name;
    private int weight;

    public WeightObject() {
    }

    public WeightObject(String number, int i) {
        this.name = number;
        this.weight = i;
    }

    public int getWeight() {
        return weight;
    }

    public String getName() {
        return name;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static int getRandomWeight(int totalWeight) {
        return new Random().nextInt(totalWeight)+1;
    }

    public static int calculateTheTotalWeights(List<Integer> weights) {
        int totalWeight = 0;
        for (int weightObject : weights) {
            totalWeight += weightObject;
        }
        return totalWeight;
    }

    public static int getTheCursorByWeights(int totalWeight, List<Integer> cursorList) {
        int randomWeight = getRandomWeight(totalWeight);
        int cumulativeWeight = 0;
//        System.out.println(randomWeight);
        for (int i = 0; i < cursorList.size(); i++) {
            cumulativeWeight += cursorList.get(i);
            if (randomWeight <= cumulativeWeight) {
                return i;

            }
        }
        return -1;
    }


    public static List<WeightObject> getWeightObject(int number, List<WeightObject> weightObjects) {
        List<Integer> cursorList = weightObjects.stream().map(weightObject -> {
            return weightObject.getWeight();
        }).collect(Collectors.toList());

        int totalWeight = calculateTheTotalWeights(cursorList);
        ArrayList<WeightObject> weightObjectArrayList = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            int theCursorByWeights = getTheCursorByWeights(totalWeight, cursorList);
            weightObjectArrayList.add(weightObjects.get(theCursorByWeights));
        }
        return weightObjectArrayList;
    }

    public static void main(String[] args) {
        List<WeightObject> weightObjects = new ArrayList<>();
        int n = 3;
        weightObjects.add(new WeightObject("1", 1));
        weightObjects.add(new WeightObject("2", 2));
        weightObjects.add(new WeightObject("3", 3));
//        weightObjects.add(new WeightObject("4", 4));
//        weightObjects.add(new WeightObject("5", 5));
//        weightObjects.add(new WeightObject("6", 6));

        List<WeightObject> weightObjects1 = getWeightObject(10000, weightObjects);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        for (int i = 0; i < weightObjects1.size(); i++) {
            WeightObject weightObject = weightObjects1.get(i);
            Integer i1 = stringIntegerHashMap.get(weightObject.getName());
            if (i1 == null) {
                stringIntegerHashMap.put(weightObject.getName(), 1);
            } else {
                stringIntegerHashMap.put(weightObject.getName(), i1 + 1);
            }
        }
        stringIntegerHashMap.forEach((k, v) -> {
            System.out.println(k + " " + v);
        });

    }
}
